<?php

require_once 'constants.php';

// returns array $notes
// $notes[i]: $note
function load_all_notes($user) {
    global $data_dir;
    $dir = $data_dir.$user['id'].'/';
    $answer = array();
    foreach (scandir($dir) as $file) {
        if ($file[0] != '.' && is_dir($dir.$file)) {
            $answer[] = load_last_note($user, $file);
        }
    }
    return $answer;
}

// returns array $note
// $note['subject']: string
// $note['body']: string
// $note['time']: long
// $note['edits']: int
// $note['public']: undef or true
function load_last_note($user, $note_id) {
    global $data_dir;
    $dir = $data_dir.$user['id'].'/'.$note_id.'/';
    $answer = array();
    $f = true;
    $edits = 0;
    foreach (scandir($dir) as $file) {
        if ($file[0] == '.') continue;
        if ($f) {
            $contents = file_get_contents($dir.$file);
            $x = strpos($contents, "\n");
            $answer['subject'] = trim(substr($contents, 0, $x));
            $answer['body'] = trim(substr($contents, $x + 1));
            $answer['time'] = decode_time($file);
            $f = false;
        }
        // TODO: maybe speed up by calculating how many files are there some other way
        $edits++;
    }
    if (file_exists($dir.'.public')) $answer['public'] = true;
    $answer['edits'] = $edits;
    return $answer;
}

// returns array $notes
// $notes[i] -- $note
function load_note($user, $note_id) {
    global $data_dir;
    $dir = $data_dir.$user['id'].'/'.$note_id.'/';
    $answer = array();
    foreach (scandir($dir) as $file) {
        if ($file[0] == '.') continue;
        $contents = file_get_contents($dir.$file);
        $x = strpos($contents, "\n");
        $note = array();
        $note['subject'] = trim(substr($contents, 0, $x));
        $note['body'] = trim(substr($contents, $x + 1));
        $note['time'] = decode_time($file);
        $note['edits'] = 0;
        $answer[] = $note;
    }
    return $answer;
}

function is_public_note($user, $note_id) {
    global $data_dir;
    return file_exists($data_dir.$user['id'].'/'.$note_id.'/.public');
}

function random_temp_dir($user, $note_id) {
    global $temp_dir;
    return $temp_dir.md5($user['id'].'-'.$note_id.'-'.rand());
}

function add_new_note($user, $subject, $body, $is_public) {
    global $data_dir;
    $body = trim($body);
    if (empty($body)) return false;
    $note_id = encode_time(time());
    $tmpd = random_temp_dir($user, $note_id);
    mkdir($tmpd, 0777, true);
    $subject = trim($subject);
    $x = strpos($subject, "\n");
    if ($x !== false) $subject = substr($subject, 0, $x);
    $note = $subject."\n".$body;
    // TODO: error-safety
    file_put_contents($tmpd.'/'.$note_id, $note);
    if ($is_public) file_put_contents($tmpd.'/.public', "");
    rename($tmpd, $data_dir.$user['id'].'/'.$note_id);
    return $note_id;
}

function update_existing_note($user, $edit_id, $subject, $body, $is_public) {
    global $data_dir, $trash_file;
    $body = trim($body);
    if (empty($body)) return false;
    $note_id = encode_time(time());
    $tmpd = random_temp_dir($user, $note_id);
    rename($data_dir.$user['id'].'/'.$edit_id, $tmpd);
    $subject = trim($subject);
    $x = strpos($subject, "\n");
    if ($x !== false) $subject = substr($subject, 0, $x);
    $note = $subject."\n".$body;
    // checking if this text is exactly the same as the last existing version
    $old = file_get_contents($tmpd.'/'.$edit_id);
    if (strcmp($note, $old) == 0) {
        $note_id = $edit_id;
    } else {
        // TODO: error-safety
        file_put_contents($tmpd.'/'.$note_id, $note);
    }
    if ($is_public) file_put_contents($tmpd.'/.public', "");
        else if (file_exists($tmpd.'/.public')) rename($tmpd.'/.public', $trash_file);
    rename($tmpd, $data_dir.$user['id'].'/'.$note_id);
    return $note_id;
}


function remove_note($user, $note_id) {
    global $data_dir, $trash_dir;
    $dir = $data_dir.$user['id'].'/'.$note_id;
    // TODO: error-safety
    rename($dir, $trash_dir.$user['id'].'_'.$note_id);
}


?>
